home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Internet Info 1994 March
/
Internet Info CD-ROM (Walnut Creek) (March 1994).iso
/
networking
/
ip
/
ka9q
/
net_des.arc
/
UUDECODE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-12-05
|
2KB
|
75 lines
/* uudecode.c - convert ascii-encoded files back to their original form
* Usage: uudecode [infile]
*
* This command differs from the regular UNIX one in that the embedded
* file name "/dev/stdout" is recognized, allowing it to be used in a pipeline
*
* Written and placed in the public domain by Phil Karn, KA9Q 31 March 1987
*/
#include <stdio.h>
#define LINELEN 80
#define MAXREC 64
main(argc,argv)
int argc;
char *argv[];
{
char linebuf[LINELEN],*index(),*fgets();
register char *cp;
register int linelen,i;
FILE *in,*out;
if(argc > 1){
if((in = fopen(argv[1],"r")) == NULL){
fprintf(stderr,"Can't read %s\n",argv[1]);
exit(1);
}
} else
in = stdin;
/* Find begin line */
while(fgets(linebuf,LINELEN,in) != NULL){
if((cp = index(linebuf,'\n')) != NULL)
*cp = '\0';
if(strncmp(linebuf,"begin",5) == 0)
break;
}
if(feof(in)){
fprintf(stderr,"No begin found\n");
exit(1);
}
/* Find beginning of file name */
cp = &linebuf[6];
if((cp = index(cp,' ')) != NULL)
cp++;
/* Set up output stream */
if(cp == NULL || strcmp(cp,"/dev/stdout") == 0){
out = stdout;
} else if((out = fopen(cp,"w")) == NULL){
fprintf(stderr,"Can't open %s\n",cp);
exit(1);
}
/* Now crunch the input file */
while(fgets(linebuf,LINELEN,in) != NULL){
linelen = linebuf[0] - ' ';
/* Some uuencodes put ` (64) instead of space (0) in the
* end-of-file record
*/
linelen &= 63;
if(linelen == 0 || strncmp(linebuf,"end",3) == 0)
break;
for(cp = &linebuf[1];linelen > 0;cp += 4){
for(i=0;i<4;i++){
cp[i] -= ' ';
cp[i] &= 63;
}
putc((cp[0] << 2) | ((cp[1] >> 4) & 0x3),out);
if(--linelen > 0)
putc((cp[1] << 4) | ((cp[2] >> 2) & 0xf),out);
if(--linelen > 0)
putc((cp[2] << 6) | cp[3],out);
linelen--;
}
}
fclose(out);
}